package net.semanticmetadata.seams;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
/*
 * This file is part of SeamCarving.
 *
 * SeamCarving is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * SeamCarving is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SeamCarving; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * ********************
 * (c) 2007 by Mathias Lux (mathias@juggle.at)
 * Palmengasse 20, 8010 Graz, Austria
 * http://www.semanticmetadata.net
 */
import javax.swing.plaf.DimensionUIResource;

public class ImageViewPanel extends JPanel implements MouseMotionListener {

    private BufferedImage img = null;
    private BufferedImage overlay;
    private int circle = 25;
    private Color retain = new Color(255, 0, 0, 64);
    private Color remove = new Color(0, 255, 0, 64);
    private Color draw = retain;

    public ImageViewPanel() {
        this.addMouseMotionListener(this);
        Thread t = new Thread(new Runnable() {

            public void run() {
                repaint();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ImageViewPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        // t.start();
    }


    public void setImage(BufferedImage img) {
        this.img = img;
        Dimension size = new DimensionUIResource(img.getWidth(), img.getHeight());
        // setPreferredSize(size);
    }

    public void setImageWithViewUpdate(BufferedImage img) {
        this.img = img;
        Dimension size = new DimensionUIResource(img.getWidth(), img.getHeight());
        setPreferredSize(size);
        overlay = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Color a = new Color(255, 255, 255, 0);
        Graphics g = overlay.getGraphics();
        g.setColor(a);
        g.fillRect(0, 0, img.getWidth(), img.getHeight());
        SwingUtilities.updateComponentTreeUI(this);
    }

    public BufferedImage getImg() {
        return img;
    }

    protected void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;
        g2.clearRect(0, 0, getWidth(), getHeight());
        if (img != null) g2.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
        if (overlay!=null) g2.drawImage(overlay, 0, 0, overlay.getWidth(), overlay.getHeight(), null);
    }

    public void mouseDragged(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        if (x >= 0 && x < img.getWidth() && y >= 0 && y <= img.getHeight()) {
            Graphics g = overlay.getGraphics();
            g.setColor(draw);
            g.fillOval(x - circle / 2, y - circle / 2, circle, circle);
        }
        repaint();
    }

    public void setBrushSize(int size) {
        this.circle = size;
    }

    public BufferedImage getOverlay() {
        return overlay;
    }

    public void enableRemoveBrush(boolean enable) {
        if (enable) {
            draw = remove;
        } else {
            draw = retain;
        }
    }

    public void mouseMoved(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}